package com.gongpingjialoan.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.LoanEnumsData;
import com.gongpingjialoan.widget.adapters.ArrayWheelAdapter;
import com.gongpingjialoan.widget.views.WheelView;

import java.util.List;

/**
 * Created by Administrator on 2017/9/25.
 */

public class SelectLoanInfoDialog extends Dialog implements View.OnClickListener {

    private Context mContext;

    private String type;

    private WheelView yearWheelView;

    private ArrayWheelAdapter<String> stringArrayWheelAdapter;

    private String[] datas;

    private OnDialogCallBack mOnDialogCallBack;

    private TextView cancleTextView, confirmTextView, tv_share_title;

    List<LoanEnumsData> data_list;
    String title;

    public SelectLoanInfoDialog(Context mContext, String title, List<LoanEnumsData> data_list, OnDialogCallBack onDialogCallBack) {
        super(mContext, R.style.dialogs);
        this.mContext = mContext;
        this.data_list = data_list;
        this.title = title;
        this.mOnDialogCallBack = onDialogCallBack;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loan_info);
        initView();
    }


    private void initView() {
        yearWheelView = (WheelView) findViewById(R.id.year_view);
        cancleTextView = (TextView) findViewById(R.id.btn_myinfo_cancel);
        confirmTextView = (TextView) findViewById(R.id.btn_myinfo_sure);
        tv_share_title = (TextView) findViewById(R.id.tv_share_title);
        tv_share_title.setText(title);
        cancleTextView.setOnClickListener(this);
        confirmTextView.setOnClickListener(this);

        datas = getDay();
        stringArrayWheelAdapter = new ArrayWheelAdapter<String>(mContext, datas);
        stringArrayWheelAdapter.setItemResource(R.layout.time_tiem);
        stringArrayWheelAdapter.setItemTextResource(R.id.text);
        yearWheelView.setViewAdapter(stringArrayWheelAdapter);

        yearWheelView.setCurrentItem(0);
        yearWheelView.setVisibleItems(3);

        yearWheelView.setCyclic(false);

    }

    @Override
    public void onClick(View view) {
        if (view == cancleTextView) {
            dismiss();
        }
        if (view == confirmTextView) {
            if (mOnDialogCallBack != null) {
                mOnDialogCallBack.onDateBack(yearWheelView.getCurrentItem());
                dismiss();
            }
        }
    }

    public String[] getDay() {
        if (data_list != null) {
            String[] mDay = new String[data_list.size()];
            for (int i = 0; i < data_list.size(); i++) {
                mDay[i] = data_list.get(i).getName();
            }
            return mDay;
        }
        return new String[0];
    }

    public interface OnDialogCallBack {
        void onDateBack(int currentItem);
    }

}
